package icom.acm.manager.mircblog;

import icom.acm.bean.mircblog.T_MB_MESSAGE;
import icom.acm.bean.mircblog.MessageInfo;
import icom.acm.util.DBConnect;

import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

public class T_MB_MessageMgr {
	public T_MB_MESSAGE insert(T_MB_MESSAGE msg){
		int flag = -1;
		String now = icom.acm.util.DateUtil.s_now("yyyy-MM-dd HH:mm:ss");
		String sql = "INSERT INTO T_MB_MESSAGE(M_ID, M_U_ID, M_CONTENT, M_DATE, MT_M_ID, M_ANNEX)" +
				" VALUES(NULL, " + msg.getM_U_ID() + ", '" +
				msg.getM_CONTENT()+"', '" + now + "', " + 
				msg.getMT_M_ID() + ", " + msg.getM_ANNEX() + ")";
		
		DBConnect db = new DBConnect();				
		db.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);				
		try {
			db.excute();
			flag = db.getGeneratedKeys();
			msg.setM_ID(flag);
			msg.setM_DATE(icom.acm.util.DateUtil.s2d("yyyy-MM-dd HH:mm:ss", now));
		} catch (MySQLIntegrityConstraintViolationException e) {
			// 数据重复
			flag = -1;
		} finally{			
			db.close();
		}
		return msg;
	}
	
	public T_MB_MESSAGE insert(T_MB_MESSAGE msg, DBConnect db) throws MySQLIntegrityConstraintViolationException{
		int flag = -1;
		String now = icom.acm.util.DateUtil.s_now("yyyy-MM-dd HH:mm:ss");
		msg.setM_DATE(icom.acm.util.DateUtil.now());
		String sql = "INSERT INTO T_MB_MESSAGE(M_ID, M_U_ID, M_CONTENT, M_DATE, MT_M_ID, M_ANNEX)" +
				" VALUES(NULL, " + msg.getM_U_ID() + ", '" +
				msg.getM_CONTENT()+"', '" + now + "', " + 
				(msg.getMT_M_ID()<0?null:msg.getMT_M_ID()) + ", " + msg.getM_ANNEX() + ")";		
		db.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);				
		db.excute();
		flag = db.getGeneratedKeys();
		msg.setM_ID(flag);
		msg.setM_DATE(icom.acm.util.DateUtil.s2d("yyyy-MM-dd HH:mm:ss", now));
		
		return msg;
	}
	
	public boolean Delete(final int MID){
		final DBConnect db = new DBConnect();
		final String sql = "DELETE FROM T_MB_MESSAGE WHERE M_ID = ?";
		boolean isSucceed = false;
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setInt(1, MID);
			isSucceed = db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
		return isSucceed;
	}
	
	/**
	 * @berif 	按作者编号得到 相关微博
	 * @param 	uid 作者编号
	 * @return 	微博列表
	 */
	public List<MessageInfo> GetCmtList(final int uid) {
		final List<MessageInfo> msgList = new ArrayList<MessageInfo>();
		final DBConnect db = new DBConnect();
		final String sql = "select T_MB_MESSAGE.*,T_USER.U_NICKNAME from " +
				"T_MB_MESSAGE,T_USER where M_U_ID = ? and" +
				" T_MB_MESSAGE.M_U_ID = T_USER.U_ID order by M_DATE desc";
		db.prepareStatement(sql);
		db.excuteQuery();
		try {
			while (db.next()) {
				final T_MB_MESSAGE msg = new T_MB_MESSAGE();
				final MessageInfo mInfo = new MessageInfo();
				msg.setM_ID(db.getInt("M_ID"));
				msg.setM_U_ID(db.getInt("M_U_ID"));
				msg.setM_CONTENT(db.getString("M_CONTENT"));
				msg.setM_DATE(db.getTimestamp("M_DATE"));
				msg.setMT_M_ID(db.getInt("MT_M_ID"));
				mInfo.setMsg(msg);
				mInfo.setAuthor(db.getString("U_NICKNAME"));
				msgList.add(mInfo);
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return msgList;
	}
	public List<T_MB_MESSAGE> selectByUidOrFriend(Integer u_id) {
		List<T_MB_MESSAGE> list = new ArrayList<T_MB_MESSAGE>();
		String sql = "";
		
		return list;
	}
}
